//
//  CompanyInfoEditView.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/18.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CompanyInfoEditView.h"
#import "StringTool.h"
#import "config.h"



@interface CompanyInfoEditView ()<UITextViewDelegate>


@property (strong, nonatomic) UIView *blckView;
@property (weak, nonatomic) IBOutlet UITextView *textView;


-(void)handleSingleTap:(UITapGestureRecognizer *)sender;


@end





@implementation CompanyInfoEditView





+(instancetype)creadInstance{
    
    CompanyInfoEditView *view = [[[NSBundle mainBundle]loadNibNamed:@"CompanyInfoEditView" owner:nil options:nil]lastObject];
    
    view.textView.delegate = view;
    [view.textView setBackgroundColor:[UIColor whiteColor]];
    
    return  view;
}



-(void)showInView:(UIView*)view{
    
    if (!self.blckView) {
        self.blckView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
        [self.blckView setBackgroundColor:[UIColor blackColor]];
        UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.blckView addGestureRecognizer:singleTap];
    }
    
    
    CGRect buf = self.frame;
    buf.size.width = view.frame.size.width;
    buf.origin.y = view.frame.size.height;
    [self setFrame:buf];
    
    [self.blckView setAlpha:0];
    
    buf.origin.y = view.frame.size.height - buf.size.height;
    [view addSubview:self.blckView];
    [view addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        
        
        [self.blckView setAlpha:0.6];
        
        [self setFrame:buf];
        
        
    } completion:^(BOOL finished) {
        
    }];
}


-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    
    [self hide];
}


-(Boolean)isShow{
    
    return self.superview!=nil;
}

-(void)hide{
    
    
    [self endEditing:YES];
    
    
    CGRect buf = self.frame;
    buf.origin.y = PMH;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        [self.blckView setAlpha:0];
        
        [self setFrame:buf];
        
    } completion:^(BOOL finished) {
        
        [self.blckView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


-(void)setText:(NSString*)data{
    
    if ([StringTool isBlankString:data]) {
        data = @"暂无数据";
    }
    [self.textView setText:data];
}



-(NSString*)getText{
    
    NSString *str = self.textView.text;
    if ([StringTool isBlankString:str]) {
        return @"";
    }else{
        return str;
    }
}


#pragma mark textView delegate
- (void)textViewDidBeginEditing:(UITextView *)textView{

    CGRect buf = self.frame;
    buf.origin.y = 0;
    buf.size.height = PMH - 64-233;
    [UIView animateWithDuration:0.2 animations:^{
        
        [self setFrame:buf];
        
    } completion:^(BOOL finished) {
        
    }];
}




@end
